package fr.xebia.monitoring.config;

import java.util.HashMap;
import java.util.Map;


public class MonitorLevel {

	private String categoryName;
    private Boolean enable;
	private MonitorLevel parent;
	private Map<String, MonitorLevel> children;

	public MonitorLevel() {
		super();
	}

	public MonitorLevel(String name) {
		this();
		this.categoryName = name;
	}

	public MonitorLevel(MonitorLevel parent) {
		this();
		this.parent = parent;
	}

	public String getCategoryName() {
		return this.categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	/**
	 * If enable status is not set, asks the parent
	 * @return
	 */
	public Boolean isEnable() {
		if (this.enable == null) {
			return parent.isEnable();
		}
		return this.enable;
	}

	public void setEnable(Boolean enable) {
		this.enable = enable;
	}

	public MonitorLevel getParent() {
		return this.parent;
	}

	public void setParent(MonitorLevel parent) {
		this.parent = parent;
	}

	public void addChild(MonitorLevel monitorLevel) {
		if (this.children == null) {
			this.children = new HashMap<String, MonitorLevel>();
		}
		children.put(monitorLevel.getCategoryName(), monitorLevel);
	}

	protected Map<String, MonitorLevel> getChildren() {
		if (this.children == null) {
			this.children = new HashMap<String, MonitorLevel>();
		}
		return this.children;
	}

	public boolean hasChild(String categoryName) {
		if (this.children == null) {
			return false;
		}
		if (children.containsKey(categoryName)) {
			return true;
		}
		return false;
	}

	public boolean hasChildren() {
		if (this.children == null) {
			return false;
		}
		if (children.size() == 0) {
			return false;
		}
		return true;
	}

	public MonitorLevel getChild(String categoryName) {
		if (this.children == null) {
			return null;
		}
		if (children.containsKey(categoryName)) {
			return children.get(categoryName);
		}
		return null;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append(categoryName);
		builder.append(":").append(enable);
		return builder.toString();
	}
}
